/*Linux系统提供了获取主机信息的函数，gethostbyname(),gethostbyaddr(),两者都会用到一个结构体，结构体信息如下：
struct hostent	{	       
	
		char    *h_name;        // official name of host     
		char    **h_aliases;    // alias list  
	        int     h_addrtype;     // host address type
		int     h_length;       // length of address         
		char    **h_addr_list;  //list of addresses 

	     }

 #define h_addr  h_addr_list[0]  //for backward compatibility 

*/

#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
int main(int argc, char *argv[])
{
	struct hostent *p;
	int i;
			 

	if (argc < 2)
		return -1;
				 
	p = gethostbyname(argv[1]);
	printf("hostname %s\n", p->h_name);
	printf("address ");
	for (i = 0; p->h_addr_list[i]; i++) 
	{
		printf("%s ", inet_ntoa(*(struct in_addr *)p->h_addr_list[i]));
	
	}
	printf("\n");
									 
	return 0;

}
